package dk.mirasola.systemtraining.bridgewidgets.shared.model.distributionfiltertree;

import com.google.gwt.user.client.rpc.IsSerializable;

public class HcpInterval implements IsSerializable {
    public static HcpInterval create(int min, int max, boolean validate) {
        if (validate) {
            HcpInterval hcpInterval = new HcpInterval();
            hcpInterval.setHcpInterval(min, max);
            return hcpInterval;
        } else {
            return new HcpInterval(min, max);
        }
    }

    public static final int HCP_INTERVAL_MIN = 0;
    public static final int HCP_INTERVAL_MAX = 40;

    private int minHcp = HCP_INTERVAL_MIN;
    private int maxHcp = HCP_INTERVAL_MAX;

    public HcpInterval() {
    }

    private HcpInterval(int minHcp, int maxHcp) {
        this.maxHcp = maxHcp;
        this.minHcp = minHcp;
    }

    public int getMinHcp() {
        return minHcp;
    }

    public int getMaxHcp() {
        return maxHcp;
    }

    /**
     * Sets hcp interval
     *
     * @param minHcp Minimum of interval
     * @param maxHcp Maximum of interval
     * @throws IllegalArgumentException if min or max hcp not between 0 and 40
     * @throws IllegalArgumentException if min hcp larger then max hcp
     */
    public void setHcpInterval(int minHcp, int maxHcp) {
        if (minHcp < HCP_INTERVAL_MIN || minHcp > HCP_INTERVAL_MAX) {
            throw new IllegalArgumentException("minHcp must be between " + HCP_INTERVAL_MIN + " and " + HCP_INTERVAL_MAX);
        }
        if (maxHcp < HCP_INTERVAL_MIN || maxHcp > HCP_INTERVAL_MAX) {
            throw new IllegalArgumentException("maxHcp must be between " + HCP_INTERVAL_MIN + " and " + HCP_INTERVAL_MAX);
        }
        if (minHcp > maxHcp) {
            throw new IllegalArgumentException("minHcp must smaller than maxHcp");
        }
        this.minHcp = minHcp;
        this.maxHcp = maxHcp;
    }

    public boolean contains(int hcp) {
        return hcp >= minHcp && hcp <= maxHcp;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        HcpInterval that = (HcpInterval) o;
        return maxHcp == that.maxHcp && minHcp == that.minHcp;
    }

    @Override
    public int hashCode() {
        return 31 * minHcp + maxHcp;
    }

    public HcpInterval copy() {
        return new HcpInterval(this.minHcp, this.maxHcp);
    }
}
